home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / 24 aspnet applications / aspnetapplications / cacheform.aspx.vb < prev    next >
Encoding:
Text File  |  2002-03-18  |  8.3 KB  |  201 lines

  1. Public Class CacheForm
  2.     Inherits System.Web.UI.Page
  3.     Protected WithEvents lblArea As System.Web.UI.WebControls.Label
  4.     Protected WithEvents lblMessage As System.Web.UI.WebControls.Label
  5.  
  6. #Region " Web Form Designer Generated Code "
  7.  
  8.     'This call is required by the Web Form Designer.
  9.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  10.  
  11.     End Sub
  12.  
  13.     Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
  14.         'CODEGEN: This method call is required by the Web Form Designer
  15.         'Do not modify it using the code editor.
  16.         InitializeComponent()
  17.     End Sub
  18.  
  19. #End Region
  20.  
  21.     Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  22.         ' Check whether xml data is cached already.
  23.         If Cache("Employees") Is Nothing Then
  24.             CacheEmployeesData()
  25.         End If
  26.         ' Get the cached data.
  27.         Dim xmldoc As System.Xml.XmlDocument = _
  28.             DirectCast(Cache("Employees"), System.Xml.XmlDocument)
  29.  
  30.         ' display all the Cache items
  31.         DisplayCacheContents()
  32.     End Sub
  33.  
  34.     ' Display the contents of the Cache object.
  35.  
  36.     Sub DisplayCacheContents()
  37.         Dim de As DictionaryEntry
  38.         Dim msg As String
  39.         For Each de In Cache
  40.             msg &= String.Format("<b>{0}</b> = {1}<br>", de.Key, de.Value)
  41.         Next
  42.         ' Display the result in a Label control.
  43.         lblMessage.Text = msg
  44.         Diagnostics.Debug.WriteLine(msg)
  45.     End Sub
  46.  
  47. End Class
  48.  
  49. ' The following functions are in a module, because they must
  50. ' be accessible from the code in Global.asax file.
  51.  
  52. ' You can test several cache options by assigning a different
  53. ' value to the following constant
  54. ' 0 = just store in the Cache object
  55. ' 1 = make the "employees" Cache item dependent on a file, and
  56. '        the "EmployeeCacheTime" item depending on "employee"
  57. ' 2 = make the "employees" item expire after an absolute timeout
  58. ' 3 = make the "employees" item expire after a timeout of inactivity
  59. ' 4 = same as (1), but specify an OnRemove callback routine
  60. ' 5 = same as (1),  but specify that the tracking shouldn't start immediately
  61. '        (this option doesn't work as documented, read remarks below)
  62.  
  63. #Const CACHE_ACTION = 0
  64.  
  65. Module CacheFunctions
  66.     Public FILENAME As String = HttpContext.Current.Server.MapPath("employees.xml")
  67.  
  68.     ' Save a reference to the Cache object.
  69.     Dim Cache As System.Web.Caching.Cache = HttpRuntime.Cache
  70.  
  71.     ' this routine reads the employees.xml file into an XmlDocument object
  72.  
  73.     Sub CacheEmployeesData()
  74.         ' Display a diagnostic message.
  75.         Diagnostics.Debug.WriteLine("Refreshing Cache Element")
  76.  
  77.         ' Read an XML document.
  78.         Dim xmldoc As New System.Xml.XmlDocument()
  79.         xmldoc.Load(FILENAME)
  80.  
  81. #If CACHE_ACTION = 0 Then
  82.         ' SIMPLE CACHING
  83.         ' Store it in the Cache object.
  84.         Cache("Employees") = xmldoc
  85.         ' Remember caching time as well.
  86.         Cache("EmployeesCacheTime") = Date.Now
  87.  
  88. #ElseIf CACHE_ACTION = 1 Then
  89.         ' Add the XML data to the cache, making it dependent on the specified file name.
  90.         Dim xmldocDep As New System.Web.Caching.CacheDependency(filename)
  91.         Cache.Insert("Employees", xmldoc, xmldocDep)
  92.  
  93.         ' Add the insertion time as well, making it dependent on the preceding item.
  94.         Dim cacheKeys() As String = {"Employees"}
  95.         Dim timeDep As New System.Web.Caching.CacheDependency(Nothing, cacheKeys)
  96.         Cache.Insert("EmployeesCacheTime", Date.Now, timeDep)
  97.  
  98. #ElseIf CACHE_ACTION = 2 Then
  99.         ' Make this item expire after 10 seconds (absolute expiration).
  100.         Cache.Insert("Employees", xmldoc, Nothing, Now.AddSeconds(10), Cache.NoSlidingExpiration)
  101.         ' Add insertion time as well.
  102.         Dim cacheKeys() As String = {"Employees"}
  103.         Dim timeDep As New System.Web.Caching.CacheDependency(Nothing, cacheKeys)
  104.         Cache.Insert("EmployeesCacheTime", Date.Now, timeDep)
  105.  
  106. #ElseIf CACHE_ACTION = 3 Then
  107.         ' Make this item expire after 10 seconds of inactivity.
  108.         Cache.Add("Employees", xmldoc, Nothing, Cache.NoAbsoluteExpiration, New TimeSpan(0, 0, 10), Caching.CacheItemPriority.Low, nothing)
  109.  
  110.         ' Add insertion time as well.
  111.         Dim cacheKeys() As String = {"Employees"}
  112.         Dim timeDep As New System.Web.Caching.CacheDependency(Nothing, cacheKeys)
  113.         Cache.Insert("EmployeesCacheTime", Date.Now, timeDep)
  114.  
  115. #ElseIf CACHE_ACTION = 4 Then
  116.         ' Add the XML data to the cache, making it dependent on the specified file name.
  117.         Dim xmldocDep As New System.Web.Caching.CacheDependency(filename)
  118.         Cache.Insert("Employees", xmldoc, xmldocDep, Nothing, Nothing, Caching.CacheItemPriority.Default, AddressOf OnRemoveItem)
  119.  
  120.         ' Add insertion time as well.
  121.         Dim cacheKeys() As String = {"Employees"}
  122.         Dim timeDep As New System.Web.Caching.CacheDependency(Nothing, cacheKeys)
  123.         Cache.Insert("EmployeesCacheTime", Date.Now, timeDep)
  124.  
  125. #ElseIf CACHE_ACTION = 5 Then
  126.         ' Add the XML data to the cache, making it dependent on the specified file name
  127.         ' but start tracking after a while
  128.         ' NOTE THAT THE FOLLOWING DOESN'T WORK AS DOCUMENTED
  129.         ' It should start the tracking after the specified timeout, but it appears to
  130.         ' start it immediately
  131.         Dim xmldocDep As New System.Web.Caching.CacheDependency(filename, Now.AddSeconds(15))
  132.         Cache.Insert("Employees", xmldoc, xmldocDep, Nothing, Nothing, Caching.CacheItemPriority.Default, AddressOf OnRemoveItem)
  133.  
  134.         ' Add insertion time as well.
  135.         Dim cacheKeys() As String = {"Employees"}
  136.         Dim timeDep As New System.Web.Caching.CacheDependency(Nothing, cacheKeys)
  137.         Cache.Insert("EmployeesCacheTime", Date.Now, timeDep)
  138. #End If
  139.  
  140.     End Sub
  141.  
  142. #If 0 Then
  143.     Sub CacheEmployeesData()
  144.         ' Read an XML document.  
  145.         Dim xmldoc As New System.Xml.XmlDocument()
  146.         xmldoc.Load(FILENAME)
  147.  
  148.         ' Make this item expire when the file is modified, and
  149.         ' call a callback procedure when this happens.
  150.         Dim xmldocDep As New System.Web.Caching.CacheDependency(FILENAME, Now.AddSeconds(10))
  151.         Cache.Insert("Employees", xmldoc, xmldocDep, Nothing, Nothing, _
  152.                 Caching.CacheItemPriority.Default, AddressOf OnRemoveItem)
  153.  
  154.         ' Add the insertion time as well, making it dependent on the preceding item.
  155.         Dim cacheKeys() As String = {"Employees"}
  156.         Dim timeDep As New System.Web.Caching.CacheDependency(Nothing, cacheKeys)
  157.         Cache.Insert("EmployeesCacheTime", Date.Now, timeDep)
  158.     End Sub
  159. #End If
  160.  
  161.     ' this routine is called when the item is removed from the Cache object
  162.  
  163.     Sub OnRemoveItem(ByVal key As String, ByVal value As Object, ByVal reason As Caching.CacheItemRemovedReason)
  164.         ' Refresh the XML file when the cached item expires.
  165.         CacheEmployeesData()
  166.     End Sub
  167.  
  168.     ' a lock object
  169.     Dim counterLock As New Object()
  170.  
  171.     ' this routine shows how you can increment a counter in the cache
  172.  
  173.     Sub IncrementCounter()
  174.         Diagnostics.Debug.WriteLine("Before SyncLock " & Now)
  175.         Diagnostics.Debug.WriteLine("After SyncLock " & Now)
  176.         SyncLock counterLock
  177.             If Cache("counter") Is Nothing Then
  178.                 Cache("counter") = 0
  179.             Else
  180.                 ' cause a long pause - its purpose is to show that other
  181.                 ' pages (in other threads) have to wait, as you can see
  182.                 ' from the messages in the Diagnostic window
  183.                 Threading.Thread.Sleep(5000)
  184.                 If CInt(Cache("counter")) > 1 Then Cache("counter") = CInt(Cache("counter")) - 1
  185.             End If
  186.         End SyncLock
  187.  
  188.         ' in this case we're using the same Cache element to lock itself
  189.         If Cache("msg") Is Nothing Then
  190.             Cache("msg") = "*"
  191.         Else
  192.             Diagnostics.Debug.WriteLine("Before SyncLock " & Now)
  193.             SyncLock Cache("msg")
  194.                 Diagnostics.Debug.WriteLine("After SyncLock " & Now)
  195.                 Threading.Thread.Sleep(5000)
  196.                 Cache("msg") = Cache("msg").ToString & "*"
  197.             End SyncLock
  198.         End If
  199.     End Sub
  200.  
  201. End Module